/*
 * Copyright (C) 2009 Leandro Aparecido <lehphyro@gmail.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.metalframework.jsf.action;

import java.lang.reflect.*;
import java.util.*;

import org.springframework.core.*;

public class ActionInvocation {
	
	private Object target;
	private Method method;
	private Object[] arguments;
	private Object returnValue;
	private Throwable throwable;

	public ActionInvocation(Object target, Method method, Object[] arguments, Object returnValue, Throwable throwable) {
		this.target = target;
		this.method = method;
		this.arguments = arguments;
		this.returnValue = returnValue;
		this.throwable = throwable;
	}
	
	public Map<String, Object> getArgumentsWithNames() {
		ParameterNameDiscoverer nameDiscoverer = new LocalVariableTableParameterNameDiscoverer();
		
		String[] parameterNames = nameDiscoverer.getParameterNames(method);
		Map<String, Object> map = new HashMap<String, Object>();
		
		for (int i = 0; i < parameterNames.length; i++) {
			map.put(parameterNames[i], arguments[i]);
		}
		
		return map;
	}
	
	public Object getTarget() {
		return target;
	}

	public void setTarget(Object target) {
		this.target = target;
	}
	
	public Method getMethod() {
		return method;
	}

	public void setMethod(Method method) {
		this.method = method;
	}
	
	public Object[] getArguments() {
		return arguments;
	}
	
	public void setArguments(Object[] arguments) {
		this.arguments = arguments;
	}
	
	public Object getReturnValue() {
		return returnValue;
	}

	public void setReturnValue(Object returnValue) {
		this.returnValue = returnValue;
	}
	
	public Throwable getThrowable() {
		return throwable;
	}

	public void setThrowable(Throwable throwable) {
		this.throwable = throwable;
	}
	
	@Override
	public String toString() {
		return getTarget() + " - " + getMethod().getName();
	}
}
